﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Globalization;
//using System.Globalization

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args) //29.11 в 2-18
        {
            string PATH = ".//..//..//patient.dbf";
            ArrayList linesArr = new ArrayList(File.ReadAllLines(PATH));

            if (args.Length != 0 && args[0].StartsWith("-"))
            {
                Program p = new Program();
                string command = args[0].Substring(1);

                if (command == "add")
                {
                    if (args.Length != 1)
                    {
                        string[] curPatient = p.parseFieldsCurPatient(args);
                        if (curPatient == null)
                        {
                            printHelp();
                            return;
                        }
                        string[] newPatient = p.addPatient(curPatient, linesArr); // -- присвоила новый ID---- //
                        string newPat = string.Join(",", newPatient);
                        File.AppendAllText(PATH, newPat + Environment.NewLine);
                        {
                            Console.WriteLine("Patient added ");
                            Console.WriteLine();
                        }
                    }
                    else
                    {
                        printHelpAdd();
                    }
                }
                else if (command == "delete" && args[1].StartsWith("-id"))
                {
                    string curID = args[1].Substring(4);
                    ArrayList linesRes = p.deletePatient(curID, linesArr);
                    string[] arrRes = p.convertArrayListToArray(linesRes);
                    File.WriteAllLines(PATH, arrRes);
                    Console.WriteLine("Patient ID:" + curID + " deleted ");
                    Console.WriteLine();

                }
                else if (command == "edit" && args[1].StartsWith("-id"))
                {
                    string curID = args[1].Substring(4);
                    string[] curPatient = p.parseFieldsCurPatient(args);
                    if (curPatient == null)
                    {
                        printHelp();
                        return;
                    }
                    string[] editedPatient = p.editCurPatient(curPatient, linesArr);
                    ArrayList linesRes = p.editPatient(editedPatient, linesArr);
                    string[] arrRes = p.convertArrayListToArray(linesRes);
                    File.WriteAllLines(PATH, arrRes);
                    Console.WriteLine("Patient ID: " + curID + " edited");
                    Console.WriteLine();
                }
                else if (command == "list")
                {
                    p.listPatient(linesArr);
                }
                else
                {
                    printHelp();
                }
            }
            else
            {
                printHelp();
            }




        }
        //-------------- methods ----------------------------------------------------- //

        static void printHelp()   // ---------- HELP -----------//
        {
            Console.WriteLine("Enter valid command:");
            Console.WriteLine("To add a patient: -add -name:Ivan -surname:Ivanov -patronymic:Ivanovich -dob:10-dec-2014");
            Console.WriteLine("To delete a patient: -delete -id:10");
            Console.WriteLine("To edit a patient: -edit -id:11 -name:Ivan -surname:Ivanov");
            Console.WriteLine("To list patients: -list");
        }
        static void printHelpAdd()   // ---------- HELP -----------//
        {
            Console.WriteLine("To add a patient: -add -name:Ivan -surname:Ivanov -patronymic:Ivanovich -dob:10-dec-2014");
            Console.WriteLine();
        }


        public string[] convertArrayListToArray(ArrayList linesArr)
        {
            string[] arr = new string[linesArr.Count];
            for (int i = 0; i < linesArr.Count; i++)
            {
                string s = string.Join(",", linesArr[i]);
                arr[i] = s;
            }
            return arr;
        }


        public string[] editCurPatient(string[] curPatient, ArrayList linesArr)
        {
            string ID = curPatient[0];
            string[] oldPatient = new string[5];
            for (int i = 0; i < linesArr.Count; i++)
            {
                if (linesArr[i].ToString().StartsWith(ID))
                {
                    string s = string.Join(",", linesArr[i]);
                    oldPatient = s.Split(',');
                    for (int j = 0; j < oldPatient.Length; j++)
                    {
                        if (curPatient[j] != null)
                        {
                            oldPatient[j] = curPatient[j];
                        }
                    }
                }
            }

            return oldPatient;
        }


        public string[] parseFieldsCurPatient(string[] args)  // --- takes args[] elements to assing fields ----//
        {
            string[] curPatient = new string[5];

            if (args[1].Length != 0)
            {
                for (int i = 1; i < args.Length; i++)
                {
                    if (args[i].StartsWith("-"))
                    {
                        string field = args[i].Substring(1);
                        if (field.StartsWith("id:"))
                        {
                            string ID = args[i].Substring(4);
                            curPatient[0] = ID;
                        }

                        else if (field.StartsWith("name:"))
                        {
                            string name = args[i].Substring(6);
                            curPatient[1] = name;

                        }
                        else if (field.StartsWith("surname:"))
                        {
                            string surname = args[i].Substring(9);
                            curPatient[2] = surname;
                        }
                        else if (field.StartsWith("patronymic:"))
                        {
                            string patronymic = args[i].Substring(12);
                            curPatient[3] = patronymic;
                        }
                        else if (field.StartsWith("dob:"))
                        {
                            string dob = args[i].Substring(5);
                            DateTime dt;
                            if (DateTime.TryParseExact(dob, "dd-MM-yyyy", null, DateTimeStyles.None, out dt))
                            {
                                string dobValid = dt.ToString("dd-MM-yyyy");
                                curPatient[4] = dobValid;
                            }
                            else
                            {
                                printHelp();
                                return null;
                            }

                        }
                        else
                        {
                            return null;
                        }
                    }
                }
            }
            return curPatient;
        }

        public string[] addPatient(string[] curPatient, ArrayList linesArr)   //------ adds patient ---//
        {
            int newID;
            if (linesArr.Count == 0)
            {
                newID = 1;
            }
            else
            {
                string lastPatientTmp = string.Join(",", linesArr[linesArr.Count - 1]);
                string[] lastPatient = lastPatientTmp.Split(',');
                newID = Convert.ToInt32(lastPatient[0]) + 1;
            }
            curPatient[0] = newID.ToString();

            return curPatient;  // возвращает string [] newPatient
        }


        public ArrayList editPatient(string[] curPatient, ArrayList linesArr)   //------- меняет строку вArrayList на новую---//
        {
            string curPatientStr = string.Join(",", curPatient);
            ArrayList linesRes = new ArrayList();
            string ID = curPatient[0];
            for (int i = 0; i < linesArr.Count; i++)
            {
                if (linesArr[i].ToString().StartsWith(ID))
                {
                    linesRes.Add(curPatientStr);
                }
                else
                {
                    linesRes.Add(linesArr[i]);
                }
            }
            return linesRes;
        }


        public ArrayList deletePatient(string curID, ArrayList linesArr)
        {
            ArrayList linesRes = new ArrayList();
            for (int i = 0; i < linesArr.Count; i++)
            {
                string tmp = linesArr[i].ToString();
                if (tmp.StartsWith(curID))
                {
                }
                else
                {
                    linesRes.Add(tmp);
                }
            }
            return linesRes;
        }


        public void listPatient(ArrayList linesArr)    // listPatient -------------------//
        {
            foreach (var elem in linesArr)
            {
                Console.Write(elem);
                Console.WriteLine();

            }
            Console.WriteLine();


            return;
        }
    }

}